﻿<?php

if (!defined('BB_ROOT')) die(basename(__FILE__));

require(INC_DIR .'bbcode.php');

$datastore->enqueue(array(
	'ranks',
));

if (empty($_GET[POST_USERS_URL]) || $_GET[POST_USERS_URL] == ANONYMOUS)
{
	bb_die($lang['NO_USER_ID_SPECIFIED']);
}
if (!$profiledata = get_userdata($_GET[POST_USERS_URL]))
{
	bb_die($lang['NO_USER_ID_SPECIFIED']);
}

if(bf($profiledata['user_opt'], 'user_opt', 'view_profile') && IS_GUEST)
{
	meta_refresh(append_sid("login.php?redirect={$_SERVER['REQUEST_URI']}", true));
    bb_die("<b>{$profiledata['username']}</b> " . $lang['FORBADE_VIEWING']);
}

$avatar_img = get_avatar($profiledata['user_avatar'], $profiledata['user_avatar_type'], !bf($profiledata['user_opt'], 'user_opt', 'allow_avatar'));

if (!$ranks = $datastore->get('ranks'))
{
	$datastore->update('ranks');
	$ranks = $datastore->get('ranks');
}
$poster_rank = $rank_image = $rank_select = '';

// [begin] Warnings Mod 1.0.7
if ( $profiledata['user_warnings'] || $profiledata['user_banned'] )
{
	if ( ( $profiledata['user_warnings'] >= $bb_cfg['warnings_per_ban'] ) || $profiledata['user_banned'] )
	{
		$poster_rank = $lang['BANNED_RANK'];
		$rank_image =  '<img src="' . $images['user_banned_rank'] . '" alt="' . $poster_rank . '" title="' . $poster_rank . '" border="0" />';

	}
	elseif ( $profiledata['user_warnings'] )
	{
		$poster_rank = sprintf($lang['WARNED_RANK'], $profiledata['user_warnings']);
		$rank_image =  '<img src="' . $images['user_warned_rank'] . '" alt="' . $poster_rank . '" title="' . $poster_rank . '" border="0" />&nbsp;';
		$rank_image = str_repeat($rank_image, $profiledata['user_warnings']);
	}
	$rank_image .= '<br />'; 

}
// [end] Warnings Mod 1.0.7

if ($user_rank = $profiledata['user_rank'] AND isset($ranks[$user_rank]))
{
	$rank_image = ($ranks[$user_rank]['rank_image']) ? '<img src="'. $ranks[$user_rank]['rank_image'] .'" alt="" title="" border="0" />' : '';
	$poster_rank = $ranks[$user_rank]['rank_title'];
}
if (IS_ADMIN)
{
	$rank_select = array($lang['NO'] => 0);
	foreach ($ranks as $row)
	{
		$rank_select[$row['rank_title']] = $row['rank_id'];
	}
	$rank_select = build_select('rank-sel', $rank_select, $user_rank);
}

if (bf($profiledata['user_opt'], 'user_opt', 'viewemail') || IS_AM)
{
	$email_uri = ($bb_cfg['board_email_form']) ? append_sid('profile.php?mode=email&amp;'. POST_USERS_URL .'='. $profiledata['user_id']) : 'mailto:'. $profiledata['user_email'];
	$email = '<a class="editable" href="'. $email_uri .'">'. $profiledata['user_email'] .'</a>';
}
else
{
	$email = '';
}

// Report
//
// Get report user module and create report link
//
include(INC_DIR ."functions_report.php");
$report_user = report_modules('name', 'report_user');

if ($report_user && $report_user->auth_check('auth_write'))
{
	$template->assign_block_vars('switch_report_user', array());
	$template->assign_vars(array(
		'U_REPORT_USER' => append_sid('report.php?mode='. $report_user->mode .'&amp;id='. $profiledata['user_id']),
		'L_REPORT_USER' => $report_user->lang['WRITE_REPORT'])
	);
}
// Report [END]

//
// Generate page
//

$signature = ($bb_cfg['allow_sig'] && $profiledata['user_sig']) ? $profiledata['user_sig'] : '';

if(bf($profiledata['user_opt'], 'user_opt', 'allow_sig'))
{
	if($profiledata['user_id'] == $userdata['user_id'])
	{
		$signature = $lang['SIGNATURE_DISABLE'];
	}
	else
	{
		$signature = '';
	}
}
else if ($signature)
{
	$signature = bbcode2html($signature);
}

switch ($profiledata['user_level']) 
 { 
 case ADMIN : 
 $profiledata['user_level'] = '<span class="colorAdmin">Администратор</span>'; 
 $style_color = ''; 
 break; 
 case MOD : 
 $profiledata['user_level'] = '<span class="colorMod">Модератор</span>'; 
 $style_color = ''; 
 break; 
 case GROUP_MEMBER : 
 $profiledata['user_level'] = '<span class="colorGroup">Участник групп</span>'; 
 $style_color = ''; 
 break; 
 case USER : 
 $profiledata['user_level'] = '<span class="nick">Пользователь</span>'; 
 $style_color = ''; 
 break; 
 default: $style_color = ''; 
 }
 
 switch ($profiledata['user_rank'])
{
	case SUPERMOD :
		$profiledata['user_level'] = '<span class="colorSuperMod">' . $lang['ONLINE_SUPERMOD'] . '</span>';
		$style_color = '';
		break;	
}

// User group
$user_id = $userdata['user_id'];
$view_user_id = $profiledata['user_id'];
$groups = array();
$sql = '
   SELECT
      g.group_id,
      g.group_name,
      g.group_type
   FROM
      ' . BB_USER_GROUP . ' as l,
      ' . BB_GROUPS . ' as g
   WHERE
      l.user_pending = 0 AND
      g.group_single_user = 0 AND
      l.user_id ='. $view_user_id.' AND
      g.group_id = l.group_id
   ORDER BY
      g.group_name,
      g.group_id';
if ( !($result = DB()->sql_query($sql)) ) message_die(GENERAL_ERROR, 'Could not read groups', '', __LINE__, __FILE__, $sql);   
while ($group = DB()->sql_fetchrow($result)) $groups[] = $group;

$template->assign_vars(array(
   'L_USERGROUPS' => $lang['USERGROUPS'],
   )
);
if (count($groups) > 0)
{
$groupsw=TRUE;
   for ($i=0; $i < count($groups); $i++)
   {
      $is_ok = false;
      //
      // groupe invisible ?
      if ( ($groups[$i]['group_type'] != GROUP_HIDDEN) || ($userdata['user_level'] == ADMIN) )
      {
         $is_ok=true;
      }
      else
      {
         $group_id = $groups[$i]['group_id'];
         $sql = 'SELECT * FROM '. BB_USER_GROUP .' WHERE group_id='.$group_id.' AND user_id='.$user_id.' AND user_pending=0';
         if ( !($result = DB()->sql_query($sql)) ) message_die(GENERAL_ERROR, 'Couldn\'t obtain viewer group list', '', __LINE__, __FILE__, $sql);
         $is_ok = ( $group = DB()->sql_fetchrow($result) );
      }  // end if ($view_list[$i]['group_type'] == GROUP_HIDDEN)
      //
      // groupe visible : afficher
      if ($is_ok)
      {
         $u_group_name = append_sid("groupcp.php?g=".$groups[$i]['group_id']);
         $l_group_name = $groups[$i]['group_name'];
         $template->assign_block_vars('groups',array(
            'U_GROUP_NAME' => $u_group_name,
            'L_GROUP_NAME' => $l_group_name,
            )
         );
      }  // end if ($is_ok)
   }  // end for ($i=0; $i < count($groups); $i++)
}  // end if (count($groups) > 0)
else
{
$groupsw = false;
}
//user group#

$template->assign_vars(array(
	'PAGE_TITLE' 	=> sprintf($lang['VIEWING_USER_PROFILE'], $profiledata['username']),
	'USERNAME' 		=> $profiledata['username'],
	'USER_LEVEL' => $profiledata['user_level'], 
    'PROFILE_USER_ID' => $profiledata['user_id'],
	'PROFILE_USER_ID' => $profiledata['user_id'],
	'USER_REGDATE' 	=> bb_date($profiledata['user_regdate'], 'Y-m-d H:i'),
	'POSTER_RANK' 	=> $poster_rank,
	'RANK_IMAGE' 	=> $rank_image,
	'RANK_SELECT' 	=> $rank_select,
	'POSTS' 		=> $profiledata['user_posts'],
	'PM' 		=> '<a href="'. append_sid('privmsg.php?mode=post&amp;'. POST_USERS_URL .'='. $profiledata['user_id']) .'">'. $lang['SEND_PRIVATE_MESSAGE'] .'</a>',
	'EMAIL' 	=> $email,
	'WWW' 		=> $profiledata['user_website'],
	'GROUPSW'   => $groupsw,
	'ICQ' 		=> $profiledata['user_icq'],
	'LAST_VISIT_TIME' => ($profiledata['user_lastvisit']) ? (bf($profiledata['user_opt'], 'user_opt', 'allow_viewonline') && !IS_ADMIN) ? $lang['HIDDEN_USER'] : bb_date($profiledata['user_lastvisit'], 'Y-m-d H:i') : $lang['NEVER'],
	'LAST_ACTIVITY_TIME' => ($profiledata['user_session_time']) ? (bf($profiledata['user_opt'], 'user_opt', 'allow_viewonline') && !IS_ADMIN) ? $lang['HIDDEN_USER'] : bb_date($profiledata['user_session_time'], 'Y-m-d H:i') : $lang['NEVER'],
	'LOCATION' => ($profiledata['user_from']) ? $profiledata['user_from'] : '',

	'USER_ACTIVE' 	=> $profiledata['user_active'],

	'OCCUPATION' 	=> $profiledata['user_occ'],
	'INTERESTS' 	=> $profiledata['user_interests'],
	'SKYPE'         => $profiledata['user_skype'],
	'GENDER'        => ($bb_cfg['gender'] && $profiledata['user_gender']) ? $lang['GENDER_SELECT'][$profiledata['user_gender']] : '',
	'BIRTHDAY'      => ($bb_cfg['birthday']['enabled'] && $profiledata['user_birthday']) ? realdate($profiledata['user_birthday'], 'Y-m-d') : '',
	'AGE'           => ($bb_cfg['birthday']['enabled'] && $profiledata['user_birthday']) ? birthday_age($profiledata['user_birthday']) : '',
	'AVATAR_IMG' 	=> $avatar_img,

	'L_VIEWING_PROFILE' => sprintf($lang['VIEWING_USER_PROFILE'], $profiledata['username']),
	'L_ABOUT_USER_PROFILE' 	=> sprintf($lang['ABOUT_USER'], $profiledata['username']),

	'U_SEARCH_USER'     => "search.php?search_author=1&amp;uid={$profiledata['user_id']}",
	'U_SEARCH_TOPICS'   => "search.php?uid={$profiledata['user_id']}&amp;myt=1",
	'U_SEARCH_RELEASES' => "tracker.php?rid={$profiledata['user_id']}#results",

	'S_PROFILE_ACTION'  => "profile.php",

	'SIGNATURE'  => $signature,
));

//bt
// Show users torrent-profile

// [begin] Warnings Mod 1.0.7
$sql = "SELECT w.*, wt.*, u.username FROM " . BB_WARNINGS . " w, " . BB_WARNINGS_TEXT . " wt, " . BB_USERS . " u WHERE w.warning_user_id = '" . $profiledata['user_id'] . "' AND wt.warning_id = w.warning_id AND u.user_id = w.warning_poster_id";
if ( !in_array($userdata['user_level'], array(ADMIN,MOD)) )
{
	$sql .= " AND (w.warning_expires > " . time() . " OR w.warning_expires = " . WARNING_INFINITY . ')';
}
$sql .= " ORDER BY w.warning_proceed ASC, w.warning_posted DESC";
$result = DB()->sql_query($sql) or message_die(GENERAL_ERROR, 'Could not insert warning information', '', __LINE__, __FILE__, $sql);

if ( DB()->num_rows($result) )
{
	$template->set_filenames(array(
		'warnings_profile_view_body' => 'warnings_profile_view_body.tpl')
	);

	$template->assign_block_vars('switch_warnings', array());
	$i = 1;
	$previous_warning_proceed = 0;
	$warning_actions_flag = FALSE;

	while ( $row = DB()->sql_fetchrow($result) )
	{
		switch ( $row['warning_type'] )
		{
			case WARNING_TYPE_WARNING:
				$warning_icon = $images['user_warned_rank'];
			break;
					
			case WARNING_TYPE_BAN:
				$warning_icon = $images['user_banned_rank'];
			break;
		}
			
		$warning_icon = '<img src="' . $warning_icon . '" border="0" alt="">';
		$warning_text = htmlspecialchars($row['warning_text']);
		$warning_post = '<a class="gensmall" href="' . append_sid('viewtopic.php?' . POST_POST_URL . '=' . $row['warning_post_id']) . '#' . $row['warning_post_id'] . '">' . $lang['WARNED_POST_LINK'] . '</a>';
		$warning_poster = '<a class="gensmall" href="' . append_sid('profile.php?mode=viewprofile&amp;' . POST_USERS_URL . '=' . $row['warning_poster_id']) . '">' . $row['username'] . '</a>';
		$warning_posted = bb_date($row['warning_posted']);
		$warning_expires = ( $row['warning_expires'] != WARNING_INFINITY ) ? bb_date($row['warning_expires']) : $lang['WT_INFINITY'];

		if ( $row['warning_proceed'] && !$previous_warning_proceed )
		{
			$i = 1;
		}

		$template->assign_block_vars('warning_row', array(
			'WARNING_NUMBER' => $i,
			'WARNING_ICON' => $warning_icon,
			'WARNING_TEXT' => $warning_text,
			'WARNING_POST' => $warning_post,
			'WARNING_POSTER' => $warning_poster,
			'WARNING_POSTED' => $warning_posted,
			'WARNING_EXPIRES' => $warning_expires
			)
		);

		if ( $row['warning_proceed'] && !$previous_warning_proceed )
		{
			$template->assign_block_vars('warning_row.proceed_separator', array());
		}

		if ( ( $row['warning_poster_id'] == $userdata['user_id'] ) || ( $userdata['user_level'] == ADMIN ) )
		{
			$warning_actions_flag = TRUE;

			$warning_actions = array();
			$warning_actions[] = '<a class="gensmall" href="warnings.php?mode=edit_warning&amp;' . POST_WARNING_URL . '=' . $row['warning_id'] . '&amp;sid=' . $userdata['session_id'] . '">' . $lang['EDIT_WARNING'] . '</a>';
			if ( !$row['warning_proceed'] )
			{
				$warning_actions[] = '<a class="gensmall" href="warnings.php?mode=remove_warning&amp;' . POST_WARNING_URL . '=' . $row['warning_id'] . '&amp;sid=' . $userdata['session_id'] . '">' . $lang['REMOVE_WARNING'] . '</a>';
			}
			$warning_actions[] = '<a class="gensmall" href="warnings.php?mode=delete_warning&amp;' . POST_WARNING_URL . '=' . $row['warning_id'] . '&amp;sid=' . $userdata['session_id'] . '">' . $lang['DELETE_WARNING'] . '</a>';

			$template->assign_block_vars('warning_row.actions', array(
				'WARNING_ACTIONS' => implode($warning_actions, '<br />')
				)
			);
		}

		$i++;
		$previous_warning_proceed = $row['warning_proceed'];
	}

	if ( $warning_actions_flag )
	{
		$template->assign_block_vars('warning_actions_header', array());
	}

	if ( ( $profiledata['user_warnings'] >= $bb_cfg['warnings_per_ban'] ) || 	$profiledata['user_banned'] )
	{
		if ( $profiledata['user_banned'] )
		{
			$warning_type = WARNING_TYPE_BAN;
			$warning_lang = 'USER_BANNED_BAN';
		}
		else
		{
			$warning_type = WARNING_TYPE_WARNING;
			$warning_lang = 'USER_BANNED_WARNINGS';
		}

		$sql = "SELECT * FROM " . BB_WARNINGS . " WHERE warning_user_id = " . $profiledata['user_id'] . " AND warning_proceed = 0 AND warning_expires != " . WARNING_INFINITY . " AND warning_type = '$warning_type' ORDER BY warning_expires ASC LIMIT 1";
		$result = DB()->sql_query($sql) or message_die(GENERAL_ERROR, 'Could not get warning information', '', __LINE__, __FILE__, $sql);

		if ( DB()->num_rows($result) )
		{
			$row = DB()->sql_fetchrow($result);
			$unban_time = bb_date($row['warning_expires']);
			$warning_lang = sprintf($lang[$warning_lang], $unban_time);
		}
		else
		{
			$warning_lang = $lang['USER_BANNED'];
		}

		$template->assign_block_vars('switch_banned', array());

		$template->assign_vars(array(
			'BANNED' => $warning_lang,	
		));
	}

	$template->assign_var_from_handle('WARNINGS', 'warnings_profile_view_body');
}
// [end] Warnings Mod 1.0.7

define('IN_VIEWPROFILE', TRUE);
include(INC_DIR .'ucp/torrent_userprofile.php');
//bt end

$template->assign_vars(array(
	'SHOW_ACCESS_PRIVILEGE' => IS_ADMIN,
	'IGNORE_SRV_LOAD'       => ($profiledata['user_level'] != USER || $profiledata['ignore_srv_load']) ? $lang['NO'] : $lang['YES'],
	'IGNORE_SRV_LOAD_EDIT'  => ($profiledata['user_level'] == USER),
));

if (IS_ADMIN)
{
	$template->assign_vars(array(
		'EDITABLE_TPLS' => true,

		'U_MANAGE'      => "profile.php?mode=editprofile&amp;u={$profiledata['user_id']}",
		'U_PERMISSIONS' => "admin/admin_ug_auth.php?mode=user&amp;u={$profiledata['user_id']}",
	));

	$ajax_user_opt = bb_json_encode(array(
		'allow_avatar'     => bf($profiledata['user_opt'], 'user_opt', 'allow_avatar'),
		'allow_sig'        => bf($profiledata['user_opt'], 'user_opt', 'allow_sig'),
		'allow_passkey'    => bf($profiledata['user_opt'], 'user_opt', 'allow_passkey'),
		'allow_pm'         => bf($profiledata['user_opt'], 'user_opt', 'allow_pm'),
		'allow_post'       => bf($profiledata['user_opt'], 'user_opt', 'allow_post'),
		'allow_post_edit'  => bf($profiledata['user_opt'], 'user_opt', 'allow_post_edit'),
		'allow_topic'      => bf($profiledata['user_opt'], 'user_opt', 'allow_topic'),
	));

	$template->assign_vars(array(
		'EDITABLE_TPLS'    => true,
		'AJAX_USER_OPT'    => $ajax_user_opt,
		'EMAIL_ADDRESS'    => htmlCHR($profiledata['user_email']),
	));
}

$user_restrictions = array();

if (bf($profiledata['user_opt'], 'user_opt', 'allow_avatar'))    $user_restrictions[] = $lang['HIDE_AVATARS'];
if (bf($profiledata['user_opt'], 'user_opt', 'allow_passkey'))   $user_restrictions[] = $lang['DOWNLOAD_TORRENT'];
if (bf($profiledata['user_opt'], 'user_opt', 'allow_pm'))        $user_restrictions[] = $lang['SEND_PM'];
if (bf($profiledata['user_opt'], 'user_opt', 'allow_post'))      $user_restrictions[] = $lang['SEND_MESSAGE'];
if (bf($profiledata['user_opt'], 'user_opt', 'allow_post_edit')) $user_restrictions[] = $lang['EDIT_POST'];
if (bf($profiledata['user_opt'], 'user_opt', 'allow_topic'))     $user_restrictions[] = $lang['NEW_THREADS'];

$template->assign_var('USER_RESTRICTIONS', join('</li><li>', $user_restrictions));

print_page('usercp_viewprofile.tpl');
